{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "65caeab7-2a1e-4f77-9947-16a93984f630",
   "metadata": {},
   "source": [
    "你可以使用定义 Query, Path 和 Cookie 参数一样的方法定义 Header 参数。\n",
    "\n",
    "为了声明headers， 你需要使用Header, 因为否则参数将被解释为查询参数。\n",
    "\n",
    "首先导入 Header:\n",
    "\n",
    "然后使用和Path, Query and Cookie 一样的结构定义 header 参数\n",
    "\n",
    "第一个值是默认值，你可以传递所有的额外验证或注释参数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "59d15324-9367-4cdb-82a2-10cdfd5cfe6e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [16572]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://0.0.0.0:8009 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:     127.0.0.1:55646 - \"GET /docs HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55646 - \"GET /openapi.json HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55645 - \"GET /items/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55709 - \"GET /items/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55759 - \"GET /items/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55762 - \"GET /items/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55764 - \"GET /items/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55766 - \"GET /items/ HTTP/1.1\" 200 OK\n",
      "INFO:     127.0.0.1:55783 - \"GET /items/ HTTP/1.1\" 200 OK\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Shutting down\n",
      "INFO:     Waiting for application shutdown.\n",
      "INFO:     Application shutdown complete.\n",
      "INFO:     Finished server process [16572]\n"
     ]
    }
   ],
   "source": [
    "import uvicorn\n",
    "from typing import Annotated\n",
    "from fastapi import FastAPI, Header\n",
    "app = FastAPI()\n",
    "@app.get(\"/items/\")\n",
    "async def read_items(user_agent: Annotated[str | None, Header()] = None):\n",
    "    return {\"User-Agent\": user_agent}\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    config = uvicorn.Config(app, host='0.0.0.0', port=8009)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71290ebd-f8de-4eba-ac82-338cf5017743",
   "metadata": {},
   "source": [
    "# requests库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3781ef55-b7ed-4087-b815-d56016b4e463",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://127.0.0.1:8009/items/' \n",
    "headers = {\"User-Agent\": \"test\"}\n",
    "res = requests.get(url, headers=headers) \n",
    "res.text\n",
    "\n",
    "# 在另一个ipynb文件中运行代码，会得到 '{\"User-Agent\":\"test\"}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dcbbf48f-9393-4f1d-bafa-e01a8e968a07",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
